package io.itit.ecp.admin.provide.action.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.MiniProgramCarouseImageQueryReq;
import io.itit.ecp.admin.provide.action.resp.MiniProgramCarouseImageResp;
import io.itit.ecp.admin.server.entity.MiniProgramCarouseImageEntity;
import io.itit.ecp.admin.server.service.IMiniProgramCarouseImageService;
import io.itit.grass.common.provide.action.req.BaseBatchIdReq;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IAdd;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.action.validation.IRequired;
import io.itit.grass.common.provide.action.validation.IUpdate;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * 小程序首页管理
 */
@Api(tags = "小程序首页管理")
@RestController
@RequestMapping("/ecp-admin/miniProgram/carouseImage")
public class MiniProgramCarouseImageController {

    @Autowired
    private IMiniProgramCarouseImageService miniProgramCarouseImageService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions("ecp-admin:miniProgramCarouseImage:search")
    public ResultResp<IPage<MiniProgramCarouseImageResp>> queryPage(@RequestBody @Validated(IQueryPage.class) MiniProgramCarouseImageQueryReq req) {
        Page<MiniProgramCarouseImageResp> page = buildQW(req).joinPage(PageUtils.getPage(req, MiniProgramCarouseImageResp.class), MiniProgramCarouseImageResp.class);
        return ResultResp.success(page);
    }

    @ApiOperation("列表查询")
    @PostMapping("queryList")
    public ResultResp<List<MiniProgramCarouseImageResp>> queryList(@RequestBody MiniProgramCarouseImageQueryReq req) {
        return ResultResp.success(buildQW(req).joinList(MiniProgramCarouseImageResp.class));
    }

    private static JoinLambdaWrapper<MiniProgramCarouseImageEntity> buildQW(MiniProgramCarouseImageQueryReq req) {
        return Joins.of(MiniProgramCarouseImageEntity.class)
                .eq(StringUtils.hasLength(req.getPosition()), MiniProgramCarouseImageEntity::getPosition, req.getPosition())
                .like(StringUtils.hasLength(req.getName()), MiniProgramCarouseImageEntity::getName, req.getName())
                .orderByAsc(MiniProgramCarouseImageEntity::getSort)
                .orderByDesc(StringUtils.hasLength(req.getSortColumn()), MiniProgramCarouseImageEntity::getCreateTm);
    }

    @GetMapping("get")
    public ResultResp<MiniProgramCarouseImageResp> get(@RequestParam String id) {
        MiniProgramCarouseImageEntity miniProgram = miniProgramCarouseImageService.getById(id);
        return ResultResp.success(ConvertUtils.convert(miniProgram, MiniProgramCarouseImageResp.class));
    }

    @ApiOperation("新增")
    @PostMapping("add")
    @RequiresPermissions("ecp-admin:miniProgramCarouseImage:add")
    public ResultResp<Boolean> add(@RequestBody @Validated({IAdd.class}) MiniProgramCarouseImageEntity req) {
        req.setStatus(Boolean.TRUE);
        return ResultResp.success(miniProgramCarouseImageService.save(req));
    }

    @ApiOperation("编辑")
    @PostMapping("update")
    @RequiresPermissions("ecp-admin:miniProgramCarouseImage:update")
    public ResultResp<Boolean> update(@RequestBody @Validated({IUpdate.class}) MiniProgramCarouseImageEntity req) {
        if (Objects.isNull(req.getStatus())) {
            req.setStatus(Boolean.TRUE);
        }
        return ResultResp.success(miniProgramCarouseImageService.updateById(req));
    }

    @ApiOperation("启用")
    @PostMapping("enable")
    @RequiresPermissions("ecp-admin:miniProgramCarouseImage:update")
    public ResultResp<Boolean> enabled(@RequestBody @Validated({IUpdate.class}) MiniProgramCarouseImageEntity req) {
        MiniProgramCarouseImageEntity imageEntity = miniProgramCarouseImageService.getById(req.getId());
        if (Objects.nonNull(imageEntity)) {
            imageEntity.setStatus(Boolean.TRUE);
        }
        return ResultResp.success(miniProgramCarouseImageService.updateById(imageEntity));
    }

    @ApiOperation("禁用")
    @PostMapping("disabled")
    @RequiresPermissions("ecp-admin:miniProgramCarouseImage:update")
    public ResultResp<Boolean> disabled(@RequestBody @Validated({IUpdate.class}) MiniProgramCarouseImageEntity req) {
        MiniProgramCarouseImageEntity imageEntity = miniProgramCarouseImageService.getById(req.getId());
        if (Objects.nonNull(imageEntity)) {
            imageEntity.setStatus(Boolean.FALSE);
        }
        return ResultResp.success(miniProgramCarouseImageService.updateById(imageEntity));
    }

    @ApiOperation("删除(根据Id删除)")
    @PostMapping("deleteById")
    @RequiresPermissions("ecp-admin:miniProgramCarouseImage:deleteById")
    public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
        return ResultResp.success(miniProgramCarouseImageService.removeById(req.getId()));
    }

    @ApiOperation("批量删除(根据Id批量删除)")
    @PostMapping("batchDelete")
    @RequiresPermissions("ecp-admin:miniProgramCarouseImage:batchDelete")
    public ResultResp<Boolean> batchDelete(@RequestBody @Validated({IRequired.class}) BaseBatchIdReq req) {
        return ResultResp.success(miniProgramCarouseImageService.removeBatchByIds(req.getIds()));
    }

}
